<template>
    <div class="section">
     <div class="cover1">
    <el-card style="width: 500px;" class="password">
      <el-form label-width="120px" size="small" :model="form" :rules="rules" ref="pass">
  
        <el-form-item label="原密码" prop="password">
          <el-input v-model="form.password" autocomplete="off" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newpass">
          <el-input v-model="form.newpass" autocomplete="off" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="repass">
          <el-input v-model="form.repass" autocomplete="off" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-card>
     </div>
    </div>
  </template>
  
  <script>

  export default {
    name: "Password",
    data() {
      return {
        form: {},
        user: sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {},
        rules: {
          password: [
            { required: true, message: '请输入原密码', trigger: 'blur' },
            { min: 3, message: '长度不少于3位', trigger: 'blur' }
          ],
          newpass: [
            { required: true, message: '请输入新密码', trigger: 'blur' },
            { min: 3, message: '长度不少于3位', trigger: 'blur' }
          ],
          repass: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 3, message: '长度不少于3位', trigger: 'blur' }
          ],
        }
      }
    },
    created() {
      this.form.username = this.user.username
    },
    methods: {
      save() {
        this.$refs.pass.validate((valid) => {
          if (valid) {  //合法
            if (this.form.newpass !== this.form.repass) {
              this.$message.error("2次输入的新密码不相同")
              return false
            }
            this.request.post("/user/password", this.form).then(res => {
              if (res.data.code === '200') {
                this.$message.success("修改密码成功，下次登录生效！")
                // this.logout()//修改密码之后退出
              } else {
                this.$message.error(res.data.msg)
              }
            })
          }
        })
      },

    }
  }
  </script>
  
  <style scoped>
  
.section {
   display: flex;
   justify-content: space-between;
   width: 100%;
   height: 642px;
   .cover1 {
     margin-top: 15px;
     margin-left: 125px;
     width: 1000px;
     height: 642px;
  
      
         }
       }
  .password{
   width: 1000px;
   height: 300px;
   position:absolute;
   margin: 100px 500px;
   background-color: #fff;
}

  </style>
  
  